<template>
  <div class="cont-add-dialog">
    <div class="header">
      <span class="title">新增标签</span>
      <span class="close" @click="emit('close')"></span>
    </div>
    <div class="dia">
      <div class="content">
        <div class="list">
          <el-form
            ref="ruleForms"
            :model="state.pm"
            :rules="rules"
            label-width="auto"
            label-position="top"
            class="xq"
          >
            <el-form-item label="标签名称" prop="tagName">
              <el-input
                v-model="state.pm.tagName"
                placeholder="请输入标签名称"
              ></el-input>
            </el-form-item>
            <el-form-item label="标签模板" prop="categoryId">
              <el-select
                v-model="state.pm.categoryId"
                placeholder="请选择标签类别"
                :disabled="props.vt == 0"
              >
                <el-option
                  v-for="item in state.lxList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>

            <!-- <el-form-item
              label="设备点位"
              v-if="
                state.pm.categoryId == '1896747873417170944' ||
                state.pm.categoryId == '1896747873421365248'
              "
              prop="sbwz"
            >
              <el-input
                v-model="state.pm.sbwz"
                placeholder="请输入设备地址"
              ></el-input>
            </el-form-item> -->
            <el-form-item
              label="添加设备"
              prop="tempDeviceId"
              v-if="
                state.pm.categoryId == '1896747873417170944' ||
                state.pm.categoryId == '1896747873421365248'
              "
            >
              <el-select
                filterable
                v-model="state.pm.tempDeviceId"
                placeholder="请选择设备"
              >
                <el-option
                  v-for="item in state.cmList"
                  :key="item.deviceId"
                  :label="item.deviceName"
                  :value="item.deviceId"
                />
              </el-select>
            </el-form-item>
            <el-form-item
              label="事件类型"
              prop="sjlx"
              v-if="state.pm.categoryId == 'zdjy'"
            >
              <el-select v-model="state.pm.sjlx" placeholder="请选择">
                <el-option
                  v-for="item in state.cmList"
                  :key="item.cameraId"
                  :label="item.cameraName"
                  :value="item.cameraId"
                />
              </el-select>
            </el-form-item>
            <template v-if="state.pm.categoryId == 'jcdlxx'">
              <el-form-item label="起点位置" prop="dlqdwz">
                <el-input
                  v-model="state.pm.dlqdwz"
                  placeholder="请输入"
                ></el-input>
              </el-form-item>
              <el-form-item label="终点位置" prop="dlzdwz">
                <el-input
                  v-model="state.pm.dlzdwz"
                  placeholder="请输入"
                ></el-input>
              </el-form-item>
              <el-form-item label="监控数量" prop="dljksl">
                <el-input
                  v-model="state.pm.dljksl"
                  placeholder="请输入"
                ></el-input>
              </el-form-item>
              <el-form-item label="车道数量" prop="dlcdsl">
                <el-input
                  v-model="state.pm.dlcdsl"
                  placeholder="请输入"
                ></el-input>
              </el-form-item>
              <el-form-item label="车道方向" prop="dlcdfx">
                <el-select v-model="state.pm.dlcdfx" placeholder="请选择">
                  <el-option
                    v-for="item in state.cmList"
                    :key="item.cameraId"
                    :label="item.cameraName"
                    :value="item.cameraId"
                  />
                </el-select>
              </el-form-item>
              <el-form-item label="选择设备点位" prop="roadDevice">
                <el-select
                  v-model="state.pm.roadDevice"
                  multiple
                  :multiple-limit="4"
                  placeholder="请选择设备"
                >
                  <el-option
                    v-for="item in state.cmList"
                    :key="item.cameraId"
                    :label="item.cameraName"
                    :value="item.cameraId"
                  />
                </el-select>
              </el-form-item>
            </template>
            <template v-if="state.pm.categoryId == '1846861780832358400'">
              <el-form-item label="出入口点位" prop="crkPoint">
                <el-select
                  v-model="state.pm.crkPoint"
                  filterable
                  placeholder="请选择设备"
                >
                  <el-option
                    v-for="item in state.crkList"
                    :key="item.gateId"
                    :label="item.gateName"
                    :value="item.gateId"
                  />
                </el-select>
                <div class="point-area" v-if="PointList.length > 0">
                  <div class="tab-content">
                    <span
                      v-for="(item, index) in PointList"
                      class="tab-btn"
                      :key="index"
                      @click="changTab(item)"
                      :title="item.cameraName"
                      :class="{ active: currentTab == item.deviceId }"
                      >设置{{ index + 1 }}</span
                    >
                  </div>
                  <div style="padding: 15px">
                    <el-form-item label="出入口">
                      <span class="text-info">{{
                        curretnTabValue.cameraInOut
                      }}</span>
                    </el-form-item>
                    <el-form-item label="设备类型">
                      <span class="text-info">{{
                        curretnTabValue.cameraType
                      }}</span>
                    </el-form-item>
                    <el-form-item label="关联设备">
                      <span class="text-info">{{
                        curretnTabValue.cameraName
                      }}</span>
                    </el-form-item>
                  </div>
                </div>
              </el-form-item>
            </template>

            <!-- 图文标签 -->
            <template v-if="state.pm.categoryId == '1846863886985334784'">
              <div class="formbgBtn" @click="addbq">
                <el-icon color="#fff" style="font-weight: 600"
                  ><Plus
                /></el-icon>
                新增信息
              </div>
              <el-form-item
                :label="'标签信息' + (index + 1)"
                :key="index"
                v-for="(item, index) in state.twList"
              >
                <img
                  class="delpng"
                  @click="delteBq(index)"
                  src="/images/add/del.png"
                />
                <el-input
                  v-model="state.twList[index].key"
                  placeholder=""
                ></el-input>
                <el-input
                  v-model="state.twList[index].value"
                  placeholder=""
                  style="margin-top: 20px"
                ></el-input>
              </el-form-item>
            </template>
            <el-form-item
              label="照片"
              v-if="state.pm.categoryId == '1846863886985334784'"
            >
              <el-upload
                :file-list="state.picList"
                list-type="picture-card"
                :http-request="upfile"
                :limit="1"
                :on-remove="picremove"
                :on-preview="preview"
                style="width: 100%"
              >
                <el-icon>
                  <Plus />
                </el-icon>
              </el-upload>
            </el-form-item>
          </el-form>
        </div>
        <div class="btns">
          <el-button class="cusb1" @click="emit('close')">取消</el-button>
          <el-button class="cusb1" @click="save">确定</el-button>
        </div>
      </div>
    </div>
    <el-image-viewer
      style="z-index: 5000 !important"
      v-if="state.imageUrl != ''"
      :on-close="closeViewer"
      :url-list="[state.imageUrl]"
    />
  </div>
</template>

<script setup>
import { ElMessage, ElMessageBox } from "element-plus";
const props = defineProps({
  vt: Number,
  vid: String,
});
const emit = defineEmits(["close", "reget"]);
const state = reactive({
  title: "标签编辑",
  pm: {
    categoryId: "1896747873417170944",
    tagName: "",
    categoryId: "",
  },
  twList: [
    {
      key: "",
      vlaue: "",
    },
  ],
  zps: [],
  lxList: [
    {
      id: "1896747873417170944",
      name: "监控普通",
      icon: "https://vp.bcdtech.cn:8787/new-port-file/jksbpt.png",
      lightenIcon: "https://vp.bcdtech.cn:8787/new-port-file/jksbpt.png",
    },
    {
      id: "1896747873421365248",
      name: "监控智能",
      icon: "https://vp.bcdtech.cn:8787/new-port-file/jksbzn.png",
      lightenIcon: "https://vp.bcdtech.cn:8787/new-port-file/jksbzn.png",
    },
    {
      id: "1846863886985334785",
      name: "休闲娱乐",
      icon: "https://vp.bcdtech.cn:8787/new-port-file/dlbq.png",
      lightenIcon: "https://vp.bcdtech.cn:8787/new-port-file/dlbq.png",
    },
    {
      id: "1846861780832358400",
      name: "出入口",
      icon: "https://vp.bcdtech.cn:8787/new-port-file/gzsb.png",
      lightenIcon: "https://vp.bcdtech.cn:8787/new-port-file/gzsb.png",
    },
    {
      id: "1846863886985334784",
      name: "通用",
      icon: "https://vp.bcdtech.cn:8787/new-port-file/hjjc.png",
      lightenIcon: "https://vp.bcdtech.cn:8787/new-port-file/hjjc.png",
    },
    {
      id: "1846861780832358401",
      name: "道路",
      icon: "https://vp.bcdtech.cn:8787/new-port-file/sqff.png",
      lightenIcon: "https://vp.bcdtech.cn:8787/new-port-file/sqff.png",
    },
  ],
  cmList: [],
  picList: [],
  imageUrl: "",
  crkList: [], //出入口点位
});

// 新增标签
const addbq = () => {
  state.twList.push({
    key: "",
    value: "",
  });
};

// 删除标签

const delteBq = (index) => {
  if (state.twList.length <= 1) {
    ElMessage.error("至少保留一个标签信息");
    return;
  }
  state.twList.splice(index, 1);
};

let formData = ref({
  categoryId: "",
  tagName: "",
  categoryId: "",
});
const rules = reactive({
  categoryId: [
    { required: true, message: "请选择标签类别", trigger: "change" },
  ],
  tagName: [{ required: true, message: "请输入标签名称", trigger: "blur" }],
  categoryId: [
    { required: true, message: "请选择模版类别", trigger: "change" },
  ],
  tempDeviceId: [
    { required: true, message: "请选择添加设备", trigger: "change" },
  ],
  crkPoint: [{ required: true, message: "请选择出入口", trigger: "change" }],
  sbwz: [{ required: true, message: "请输入设备点位", trigger: "blur" }],
  sjlx: [{ required: true, message: "请选择事件类型", trigger: "change" }],
});
// const moptions = [{ value: 'WZ', label: '文字版' }, { value: 'TW', label: '图文版' }, { value: 'SP', label: '视频版' }, { value: 'ZP', label: '抓拍视频版' }, { value: 'CBTKQ', label: '船舶停靠区' }, { value: 'JMQ', label: '居民区' }, { value: 'TCC', label: '停车场' }, { value: 'JYXF', label: '救援消防' }, { value: 'BHQ', label: '保护区' }, { value: 'BGQ', label: '办公区' }];
const moptions = [
  { value: "1896747873417170944", label: "普通监控设备" },
  { value: "1896747873421365248", label: "智能设备监控" },
  { value: "zdjy", label: "监控设备占道经营" },
  { value: "jcdlxx", label: "基础地理信息" },
  { value: "crk", label: "出入口" },
  // { value: "xxyl", label: "休闲娱乐" },
  // { value: "WZ", label: "文字版" },
  // { value: "TW", label: "图文版" },
  // { value: "SP", label: "视频版" },
  // { value: "ZP", label: "抓拍视频版" },
  // { value: "CBTKQ", label: "船舶停靠区" },
  // { value: "JMQ", label: "居民区" },
  // { value: "TCC", label: "停车场" },
  // { value: "JYXF", label: "救援消防" },
  // { value: "BHQ", label: "保护区" },
  // { value: "BGQ", label: "办公区" },
];

let ruleForms = ref();
const save = () => {
  console.log(state.pm);
  ruleForms.value.validate((valid, fields) => {
    if (!valid) return;
    let store = useMyStore();
    let dxm = store.params.dxm || {};
    if (!dxm.type) {
      ElMessage.error("获取坐标数据异常");
      return;
    }
    state.pm.graphType =
      dxm.type == 1 ? "point" : dxm.type == 2 ? "line" : "polygon";
    let graphData = {};
    graphData.type = dxm.type;
    let w = dxm.w;
    let h = dxm.h;
    let data = Array.from(dxm.data);
    for (let i = 0; i < data.length; i++) {
      data[i].x = (1.0 * data[i].x) / w;
      data[i].y = (1.0 * data[i].y) / h;
    }
    graphData.data = data;
    state.pm.graphData = JSON.stringify(graphData);
    state.pm.deviceId = dxm.vid; //当前视频id，AR高点id
    state.pm.ptzPanpos = dxm.ptz.p;
    state.pm.ptzTiltpos = dxm.ptz.t;
    state.pm.ptzZoompos = dxm.ptz.z;
    console.log(state.pm);
    if (state.pm.categoryId == "1846863886985334784") {
      state.pm.templateCapture = JSON.stringify(state.twList);
    }
    // if (state.pm.categoryId == "1846863886985334784") {
    //   if (!state.pm.tempPic) {
    //     ElMessage.error("请上传图片");
    //     return;
    //   }
    // }
    // 处理出入口的数据
    if (state.pm.categoryId == "1846861780832358400") {
      let arr = PointList.value;
      let obj = [
        {
          label: "face",
          types: [],
        },
        {
          label: "vehicle",
          types: [],
        },
      ];
      for (let i = 0; i < arr.length; i++) {
        if (arr[i].cameraInOut == "入口" && arr[i].cameraType == "人员抓拍") {
          obj[0].types.push({
            type: "out",
            device: {
              deviceName: arr[i].cameraName,
              deviceId: arr[i].deviceId,
            },
          });
        }
        if (arr[i].cameraInOut == "出口" && arr[i].cameraType == "人员抓拍") {
          obj[0].types.push({
            type: "out",
            device: {
              deviceName: arr[i].cameraName,
              deviceId: arr[i].deviceId,
            },
          });
        }

        if (arr[i].cameraInOut == "入口" && arr[i].cameraType == "车辆抓拍") {
          obj[1].types.push({
            type: "in",
            device: {
              deviceName: arr[i].cameraName,
              deviceId: arr[i].deviceId,
            },
          });
        }
        if (arr[i].cameraInOut == "出口" && arr[i].cameraType == "车辆抓拍") {
          obj[1].types.push({
            type: "out",
            device: {
              deviceName: arr[i].cameraName,
              deviceId: arr[i].deviceId,
            },
          });
        }
      }
      state.pm.templateCapture = JSON.stringify(obj);
    }
    //保存数据
    useApi
      .post("/dynamicview/ar/camera/settag", state.pm)
      .then((res) => {
        emit("reget");
        window.parent.postMessage(
          {
            type: "iframe",
            data: {
              isrefresh: "load",
            },
          },
          "https://vp.bcdtech.cn:8787"
        );
      })
      .catch((err) => {
        ElMessage.error(err.data.message);
      });
  });
};
const upfile = (config) => {
  let data = new FormData();
  data.append("file", config.file);
  useApi
    .post("/dynamicview/ar/picupload", data)
    .then((res) => {
      state.pm.tempPic = res.data.picPath;
      state.picList = [{ name: "pic", url: res.data.picPath }];
    })
    .catch((err) => {
      ElMessage.error(err.status + " " + err.statusText);
      state.pm.tempPic = "";
      state.picList = [];
    });
};
const picremove = () => {
  state.pm.tempPic = "";
  state.picList = [];
};
const preview = (up) => {
  state.imageUrl = up.url;
};
const closeViewer = () => {
  state.imageUrl = "";
};
// const getLeixing = () => {
//   useApi
//     .post("/arhome/system/tagcategory/list", {
//       tagCategoryName: "",
//       tagCategoryState: null,
//     })
//     .then((res) => {
//       state.lxList = res.data.tagCategory4SysList || [];
//     });
// };
const getLeixing = () => {
  useApi
    .post("/dynamicview/ar/getTagCategorys", {
      tagCategoryName: "",
      tagCategoryState: null,
    })
    .then((res) => {
      state.lxList = res.data.tagCategoryList || [];
    });
};

watch(
  () => state.pm.categoryId,
  (newval, oldval) => {
    if (state.pm.categoryId === "1896747873421365248") {
      // 智能监控
      getCamara("30");
    }
    if (state.pm.categoryId === "1896747873417170944") {
      // 普通监控
      getCamara("40");
    }
  }
);

let PointList = ref([
  // { name: "人员", value: "1" },
  // { name: "车辆", value: "2" },
  // { name: "船舶", value: "3" },
]);
watch(
  () => state.pm.crkPoint,
  (newval, oldval) => {
    if (newval) {
      let index = state.crkList.findIndex((item) => item.gateId === newval);
      if (index > -1) {
        PointList.value = state.crkList[index].entranceCameras || [];
        changTab(PointList.value[0]);
      }
    } else {
      PointList.value = [];
      currentTab.value = "";
      curretnTabValue.value = {};
    }
  }
);

const getCamara = (type) => {
  useApi
    .post("/dynamicview/ar/getspeclowsbyhigh", {
      deviceId: props.vid,
      cameraType: type, //30智能 40普通
    })
    .then((res) => {
      state.pm.tempDeviceId = "";
      state.cmList = [...res.data.lowCameraList];
    });
};

let currentTab = ref("");
let curretnTabValue = ref({});
const changTab = (item) => {
  currentTab.value = item.deviceId;
  curretnTabValue.value = item;
};

const getCrklist = () => {
  useApi
    .post("/dynamicview/ar/getentrancelist", {
      deviceId: props.vid,
    })
    .then((res) => {
      state.crkList = res.data.entranceList || [];
    });
};

onMounted(() => {
  if (props.vt == 0) {
    state.title = "标签信息";
  } else {
    state.title = "标签编辑";
  }
  getLeixing();
  getCamara();
  getCrklist();
});
</script>

<style lang="scss" scoped>
.cont-add-dialog {
  width: 400px;
  height: 540px;
  background: url("/images/new/tag/tagbg.png") no-repeat;
  background-size: 100% 100%;
  .header {
    width: 100%;
    height: 52.5px;
    position: relative;
    background: url("/images/new/tag/header.png") no-repeat;
    background-size: 100% 100%;
    text-shadow: 0px 4px 4px rgba(0, 0, 0, 0.25);
    .title {
      display: inline-block;
      height: 100%;
      padding-left: 25px;
      font-family: YouSheBiaoTiHei;
      font-size: 25px;
      color: #ffffff;
      line-height: 60px;
    }

    .close {
      width: 40px;
      height: 40px;
      position: absolute;
      right: 16px;
      top: 20.5px;
      line-height: 40px;
      text-align: center;
      color: #fff;
      font-size: 30px;
      cursor: pointer;
    }
  }
}

.dia {
  width: 100%;
  height: calc(100% - 52.5px);
  // display: flex;
  // flex-direction: column;
  padding: 0 30px 20px 30px;
  .content {
    width: 100%;
    height: 100%;
    .el-select,
    .el-input {
      height: 45px;
    }
    .list {
      width: 100%;
      height: calc(100% - 67px);
      padding: 10px 10px;
      display: flex;
      flex-direction: column;
      overflow-y: auto;
      .txt {
        height: 28px;
        display: flex;
        align-items: center;
        font-weight: 600;
        font-size: 14px;
        color: #ffffff;
      }

      .sszp {
        display: flex;
        justify-content: space-between;

        .zpone {
          width: 80px;
          height: 90px;
          display: flex;
          justify-content: center;
          align-items: center;
          background: linear-gradient(
            0deg,
            rgba(11, 81, 127, 0.5) 0%,
            rgba(11, 81, 127, 0.5) 100%
          );
          box-shadow: inset 0px 0px 16px 0px rgba(0, 61, 99, 0.87);

          .empimg {
            width: 28px;
            height: 28px;
          }

          .img {
            width: 80px;
            height: 90px;
          }
        }
      }
    }
    .btns {
      margin-top: 20px;
      height: 47px;
      display: flex;
      justify-content: center;
      align-items: center;
      .cusb1,
      .cusb2 {
        width: 40%;
        height: 47px;
        background: url("/images/add/btnbg.png") no-repeat;
        background-size: 100% 100%;
      }
    }
    .point-area {
      margin-top: 10px;
      width: 100%;
      // height: 350px;
      background: url("/images/add/pointareabg.png") no-repeat;
      background-size: 100% 100%;
      .tab-content {
        width: 100%;
        height: 40px;
        background: url("/images/add/tabbg.png") no-repeat;
        background-size: 100% 100%;
        display: flex;
        align-items: center;
        padding: 0px 4px;
        position: relative;
        gap: 0 20px;
        .tab-btn {
          width: 115px;
          height: 100%;
          text-align: center;
          line-height: 36px;
          font-family: YouSheBiaoTiHei;
          font-size: 21px;
          color: #ffffff;
          cursor: pointer;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
          &.active {
            border-bottom: 4px solid #18e1ff;
            // &::after {
            //   contain: "";
            //   position: absolute;
            //   bottom: 0px;
            //   left: 0px;
            //   width: 100%;
            //   height: 4px;
            //   background: #18e1ff;
            // }
            // background: url("/images/new/tag/tab.png") no-repeat;
            // background-size: 100% 100%;
          }
        }
      }
      .text-info {
        width: 100%;
        height: 45px;
        text-align: left;
        line-height: 45px;
        font-size: 20px;
        padding-left: 12px;
        color: #a6e2fc;
        box-shadow: inset 0px 0px 10px 0px #005097;
      }
    }
  }
}

:deep(.el-form-item__label) {
  font-family: PingFangSC, PingFang SC;
  font-weight: 600;
  font-size: 20px;
  color: #ffffff;
}

:deep(.el-select__placeholder) {
  span {
    font-family: PingFangSC, PingFang SC;
    // font-size: 20px;
    color: #a6e2fc;
  }
}

:deep(.el-select) {
  height: 45px !important;
  // width: 260px;
  --el-input-text-color: #ffffff;

  .el-select__wrapper {
    height: 100%;
    background: #067cc0;
    color: #ffffff;
    border: none;
    box-shadow: none;
    border-radius: 0px !important;
  }
}

:deep(.el-input) {
  --el-input-text-color: #ffffff;
  --el-input-bg-color: #067cc0;
}

:deep(.el-textarea) {
  --el-input-bg-color: #067cc0;
  --el-input-text-color: #ffffff;
}

:deep(.el-upload) {
  width: 320px !important;
  height: 150px !important;
  border-radius: 0px !important;
  background: #067cc0;
  border: none;
}

:deep(.el-upload-list) {
  width: 100% !important;
}

:deep(.el-upload-list--picture-card .el-upload-list__item) {
  width: 100% !important;
  height: 150px !important;
}

:deep(.el-upload-list--picture-card) {
  :nth-child(even) {
    display: none;
  }
}

.formbgBtn {
  width: 170px;
  height: 46px;
  text-align: center;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: PingFangSC, PingFang SC;
  font-weight: 600;
  font-size: 20px;
  color: #ffffff;
  background: url("/images/add/formbg.png") no-repeat;
  background-size: 100% 100%;
  margin-bottom: 10px;
  cursor: pointer;
  i {
    margin-right: 8px;
    color: #fff;
  }
}
.delpng {
  width: 25px;
  height: 25px;
  cursor: pointer;
  // float: right;
  position: absolute;
  // margin-top: -40px;
  right: 0;
  top: -30px;
}
</style>